package com.qingpeng.pay.fragment;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qingpeng.pay.R;
import com.qingpeng.pay.activity.TextActivity;
import com.qingpeng.pay.adapter.MessageAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.base.BaseFragment;
import com.qingpeng.pay.beans.MessageBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.ToastUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NoticeListFragment extends BaseFragment {

    private boolean isCreated = false;
    private List<MessageBean.DataProductBean.MessageinfoProductBean> mMessageinfo;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_notice_list;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.recycler;
    }

    @Override
    public void onResume() {
        super.onResume();
        showData();
    }

    private void showData() {
        Call<MessageBean> zfnotion = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).Zfnotion();
        zfnotion.enqueue(new Callback<MessageBean>() {
            @Override
            public void onResponse(Call<MessageBean> call, Response<MessageBean> response) {
                MessageBean body = response.body();
                if (body.getStatus()!=1){
                    ToastUtils.showToast(mContext,body.getInfo());
                    return;
                }
                mMessageinfo = body.getData().getMessageinfo();
                initAdapter();
            }

            @Override
            public void onFailure(Call<MessageBean> call, Throwable t) {
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
            }
        });
    }



    @Override
    protected void initAdapter() {
        mAdapter=new MessageAdapter(R.layout.item_message);
        mAdapter.setNewData(mMessageinfo);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final String content = mMessageinfo.get(position).getContent();
                Call<NullBean> setifread = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).setifread(mMessageinfo.get(position).getId());
                setifread.enqueue(new Callback<NullBean>() {
                    @Override
                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                        NullBean body = response.body();
                        if (body.getStatus()!=1){
                            ToastUtils.showToast(mContext,body.getInfo());
                            return;
                        }
                        startActivity(new Intent(mContext, TextActivity.class)
                                .putExtra("type","通知")
                                .putExtra("string",content));
                    }
                    @Override
                    public void onFailure(Call<NullBean> call, Throwable t) {
                        ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
                    }
                });
            }
        });
        super.initAdapter();
    }
}
